package bzapps.weynak;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.util.Log;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Projection;

public class MyOverlay extends MyLocationOverlay {

    private static final int INITIAL_ZOOM = 15;

    private final MapView map;

    private final Context context;

    private final String name;

    private final OnSaveMyPosition onSave;

    /* for Archos 5IT */
    private boolean bugged = false;

    private Paint accuracyPaint;

    private Point center;

    private Point left;

    private Drawable drawable;

    private int width;

    private int height;

    @Override
    protected void drawMyLocation(Canvas canvas, MapView mapView, Location lastFix, GeoPoint myLoc, long when)
    {
        if (!bugged) {
            try {
                super.drawMyLocation(canvas, mapView, lastFix, myLoc, when);
            } catch (Exception e) {
                bugged = true;
            }
        }

        if (bugged) {
            if (drawable == null) {
                accuracyPaint = new Paint();
                accuracyPaint.setAntiAlias(true);
                accuracyPaint.setStrokeWidth(2.0f);

                drawable = mapView.getContext().getResources().getDrawable(R.drawable.enable);
                width = drawable.getIntrinsicWidth();
                height = drawable.getIntrinsicHeight();
                center = new Point();
                left = new Point();
            }
            Projection projection = mapView.getProjection();

            double latitude = lastFix.getLatitude();
            double longitude = lastFix.getLongitude();
            float accuracy = lastFix.getAccuracy();

            float[] result = new float[1];

            Location.distanceBetween(latitude, longitude, latitude, longitude + 1, result);
            float longitudeLineDistance = result[0];

            GeoPoint leftGeo = new GeoPoint((int) (latitude * 1e6), (int) ((longitude - accuracy / longitudeLineDistance) * 1e6));
            projection.toPixels(leftGeo, left);
            projection.toPixels(myLoc, center);
            int radius = center.x - left.x;

            accuracyPaint.setColor(0xff6666ff);
            accuracyPaint.setStyle(Style.STROKE);
            canvas.drawCircle(center.x, center.y, radius, accuracyPaint);

            accuracyPaint.setColor(0x186666ff);
            accuracyPaint.setStyle(Style.FILL);
            canvas.drawCircle(center.x, center.y, radius, accuracyPaint);

            drawable.setBounds(center.x - width / 2, center.y - height / 2, center.x + width / 2, center.y + height / 2);
            drawable.draw(canvas);
        }
    }

    /* for Archos 5IT */

    interface OnSaveMyPosition {
        void onSave(Location location);
    }

    public MyOverlay(String name, Context context, MapView mapView, OnSaveMyPosition onSave)
    {
        super(context, mapView);
        this.map = mapView;
        this.context = context;
        this.name = name;
        this.onSave = onSave;
    }

    @Override
    protected boolean dispatchTap()
    {
        Toast.makeText(context, name, Toast.LENGTH_LONG).show();
        return true;
    }

    @Override
    public void onLocationChanged(Location location)
    {
        super.onLocationChanged(location);
        onSave.onSave(location);
    }

    public void centerTo(GeoPoint point)
    {
        try {
            map.getController().animateTo(point);
            map.getController().setZoom(INITIAL_ZOOM);
        } catch (NullPointerException e) {
            Log.e("preload.error", "preLoad again..");
        }
    }

    public void centerMe(Location location)
    {
        Double lat = location.getLatitude() * 1E6;
        Double lng = location.getLongitude() * 1E6;
        GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());
        centerTo(point);
    }

    public void centerMe(boolean save)
    {
        Location lastFix = getLastFix();
        if (lastFix != null) {
            centerMe(lastFix);
            if (save && onSave != null) {
                onSave.onSave(lastFix);
            }
        } else {
            Toast.makeText(context, "notyet", Toast.LENGTH_SHORT).show();
        }
    }
}
